//
//  GaoPageView.m
//  DuTe
//
//  Created by Gikki Ares on 2020/12/15.
//  Copyright © 2020 vgemv. All rights reserved.
//

#import "JsPageGroupView.h"
@interface JsPageGroupView()

{
	//当前的页面数量.
	int mi_pageNum;
	//当前的页面
	int mi_currentPage;
	NSMutableArray <UIView *>* marr_page;
	float mf_pageWidth;
	float mf_pageHeight;
	UIScrollView * muiScrollView;
}

@end

@implementation JsPageGroupView


- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (void)awakeFromNib {
	[super awakeFromNib];
	[self commonInit];
}

- (void)commonInit {
	mf_pageWidth = self.bounds.size.width;
	mf_pageHeight = self.bounds.size.height;
	muiScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
	muiScrollView.pagingEnabled = YES;
	muiScrollView.bounces = NO;
	muiScrollView.scrollEnabled = NO;
	muiScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
	
	//MARK: 这个会导致scrollView自动设置insets.
	muiScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
	//MARK: ScrollView会自动设置Insets,需要关闭.
	if (@available(iOS 13.0, *)) {
		muiScrollView.automaticallyAdjustsScrollIndicatorInsets = NO;
	} else {
			// Fallback on earlier versions
	}
	marr_page = [NSMutableArray array];
	[self addSubview:muiScrollView];
}

/**
 约束让frame发生改变.将会调用layoutSubviews;
 */
- (void)layoutSubviews {
	[super layoutSubviews];
	mf_pageWidth = self.frame.size.width;
	mf_pageHeight = self.frame.size.height;
	self.backgroundColor = [UIColor whiteColor];
	muiScrollView.backgroundColor = [UIColor whiteColor];
	muiScrollView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
//	NSLog(@"%@,The frame is %@", NSStringFromClass(self.class),NSStringFromCGRect(self.frame));
	[marr_page enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//		obj.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
		obj.frame = CGRectMake(mf_pageWidth * idx, 0, mf_pageWidth, mf_pageHeight);
	}];
	[muiScrollView setContentSize:CGSizeMake(mf_pageWidth * mi_pageNum, mf_pageHeight)];
	
	[self scrollToCurrentPage:NO];
}

- (void)scrollToCurrentPage:(BOOL)animated {
	[muiScrollView scrollRectToVisible:CGRectMake(mf_pageWidth*mi_currentPage, 0, mf_pageWidth, mf_pageHeight) animated:animated];
}

- (void)choosePageAtIndex:(int)index {
	mi_currentPage = index;
	[muiScrollView scrollRectToVisible:CGRectMake(mf_pageWidth*index, 0, mf_pageWidth, mf_pageHeight) animated:YES];
//	switch (index) {
//		
//	}
}

#pragma mark 2 Interface
- (void)setDelegate:(id<JsPageGroupViewDelegate>)delegate {
	_delegate = delegate;
	[self reload];
}

- (void)reload {
	if(_delegate&&[_delegate respondsToSelector:@selector(jsPageGroupViewSetPageNum:)]) {
		mi_pageNum = [_delegate jsPageGroupViewSetPageNum:self];
	}
	
	for(int i = 0;i<mi_pageNum;i++) {
		if(_delegate&&[_delegate respondsToSelector:@selector(jsPageGroupView:viewForPageAtIndex:)]) {
			UIView * view_page = [_delegate jsPageGroupView:self viewForPageAtIndex:i];
			[marr_page addObject:view_page];
			[muiScrollView addSubview:view_page];
		}
	}
}

@end
